<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-box-img">
        <!--<img src="@/assets/login.png" alt="" />-->
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">湖南智慧政务</h3>
        </div>
        <div class="title-small">
          帐号登录
        </div>
        <div class="title-underline"></div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="zhanghao" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            clearable
            auto-complete="off"
            style='padding-right:150px;'
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="mima" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="off"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

          <div style="display: flex; align-items: center;margin-bottom: 25px">
            <el-form-item prop="captchaCode" style="flex: 1;margin: 0">
              <span class="svg-container">
                <svg-icon icon-class="yanzhengma" />
              </span>
              <el-input
                ref="captchaCode"
                v-model="loginForm.captchaCode"
                placeholder="请输入验证码"
                name="captchaCode"
                tabindex="2"
                clearable
                style="border: 1px"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
            <div class="captcha-img" @click="getCaptcha">
              <img :src="captchaSrc" alt="" />
            </div>
            <el-button type="text" @click="getCaptcha">换一张</el-button>
          </div>

        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin"
          >登录</el-button
        >
      </el-form>
    </div>
    <div class="footer">
      湖南智慧政务区块链信息科技有限公司
    </div>
  </div>
</template>

<script>
import { guid } from "@/utils";
export default {
  name: "Login",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位数字"));
      } else {
        callback();
      }
    };
    const validateCaptchaCode = (rule, value, callback) => {
      if (value.length !== 4) {
        callback(new Error("请输入正确的验证码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
        randomStr: "",
        captchaCode: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ],
        captchaCode: [
          { required: true, trigger: "blur", validator: validateCaptchaCode }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      captchaSrc: ""
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCaptcha();
  },
  methods: {
    getCaptcha() {
      this.loginForm.randomStr = guid();
      console.log(process.env.VUE_APP_BASE_API);
      // this.captchaSrc = `${process.env.VUE_APP_BASE_API}/captcha/${this.loginForm.randomStr}`;
      this.captchaSrc = `${process.env.VUE_APP_BASE_API}/captcha/${this.loginForm.randomStr}`;
      console.log(this.captchaSrc);
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(res => {
              this.$router.push({ path: "/" });
              this.loading = false;
            })
            .catch(err => {
              //this.getCaptcha();
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$cursor: #295ec8;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    flex: 1;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #295ec8;
      height: 100%;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #4e98d9;
    background: #fff;
    border-radius: 2px;
    color: #454545;
    margin-bottom: 25px;
    .el-form-item__content {
      display: flex;
    }
  }
}
</style>

<style lang="scss" scoped>
//$bg: #478ee8;
//$bg: red;
$main-color: #295ec8;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  height: 100%;
  background: url('../../assets/log.jpg') no-repeat center center;
  background-size:cover;
  overflow: hidden;
  display: flex;
  display: -ms-flexbox;
  -ms-flex-pack: center;
  -ms-flex-align: center;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  .login-box {
    display: flex;
    width: 1305px;
    height: 714px;
    //background: #ffffff;
    border-radius: 5px;
    align-items: center;
    padding: 20px;
    .login-box-img {
      //padding: 0 30px;
      width:420px;
      height:461px;
    }
  }
  .login-form {
    position: relative;
    max-width: 100%;
    overflow: hidden;
    background:white;
    padding: 10px 30px;
  }
  .svg-container {
    padding: 6px 5px;
    color: #fff;
    background-color: #295ec8;
    vertical-align: middle;
    width: 52px;
    display: inline-block;
    text-align: center;
    font-size: 24px;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 36px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: rgba(0, 0, 0, 0.85);
      line-height: 36px;
    }
  }
  .title-small {
    font-size: 22px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.85);
    line-height: 30px;
  }
  .title-underline {
    width: 86px;
    height: 5px;
    background: #295ec8;
    margin-bottom: 30px;
  }
  .captcha-img {
    width: 83px;
    height: 52px;
    border: 1px solid #4e98d9;
    margin: 0 5px;
    cursor: pointer;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .footer {
    position: fixed;
    height: 22px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: white;
    line-height: 22px;
    width: 244px;
    bottom: 68px;
    left: calc(50% - 122px);
  }
}
</style>
